package com.example.administrator.editdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.JsonWriter;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class MainActivity extends Activity {
    private DownloadManager downloadManager;
    private long mTaskId;
    private static final String JSON =
            "{" +
                    "  \"phone\" : [\"12345678\", \"87654321\"]," +
                    "  \"name\" : \"yuanzhifei89\"," +
                    "  \"age\" : 100," +
                    "  \"address\" : { \"country\" : \"china\", \"province\" : \"jiangsu\" }," +
                    "  \"married\" : false," +
                    "}";
    //创建下载任务,downloadUrl就是下载链接

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn);


//        final Uri uri = Uri.parse("http://gdown.baidu.com/data/wisegame/38cbb321c273886e/YY_30086.apk");
        final Uri uri = Uri.parse("http://bmob-cdn-16931.b0.upaiyun.com/2018/07/26/554aa95940f6a65680fae1043b10f1fc.apk");
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        startActivity(intent);

        AlertDialog.Builder dialog =
                new AlertDialog.Builder(MainActivity.this);
        dialog.setTitle("版本更新");
        dialog.setMessage("更新至新的版本");
        dialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        beforeUpdateWork();
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);
                    }
                });
        dialog.setNegativeButton("",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        dialog.show();
        final EditText text1 = findViewById(R.id.edit);
        final EditText text2 = findViewById(R.id.edit2);
        final Drawable drawable = getResources().getDrawable(R.drawable.unse);
        drawable.setBounds(-30, 0, 50, 60);
        text1.setCompoundDrawables(null, null, drawable, null);
        final Drawable drawable1 = getResources().getDrawable(R.drawable.pa);
        drawable1.setBounds(-30, 0, 50, 60);
        text2.setCompoundDrawables(null, null, drawable1, null);
        text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (hasWindowFocus()) {
                    text2.setCompoundDrawables(null, null, drawable1, null);
                    Drawable drawable = getResources().getDrawable(R.drawable.se);
                    drawable.setBounds(-30, 0, 50, 60);
                    text1.setCompoundDrawables(null, null, drawable, null);
                }
            }
        });
        text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (hasWindowFocus()) {
                    text1.setCompoundDrawables(null, null, drawable, null);
                    Drawable drawable1 = getResources().getDrawable(R.drawable.unpa);
                    drawable1.setBounds(-30, 0, 50, 60);
                    text2.setCompoundDrawables(null, null, drawable1, null);
                }
            }
        });

    }

//    //使用系统下载器下载
//    private void downloadAPK(String versionUrl, String versionName) {
//        //创建下载任务
//        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(versionUrl));
//        request.setAllowedOverRoaming(false);//漫游网络是否可以下载
//
//        //设置文件类型，可以在下载结束后自动打开该文件
//        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
//        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(versionUrl));
//        request.setMimeType(mimeString);
//
//        //在通知栏中显示，默认就是显示的
//        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
//        request.setVisibleInDownloadsUi(true);
//
//        //sdcard的目录下的download文件夹，必须设置
//        request.setDestinationInExternalPublicDir("/Browser/", versionName);
//        //request.setDestinationInExternalFilesDir(),也可以自己制定下载路径
//
//        //将下载请求加入下载队列
//        downloadManager = (DownloadManager) MainActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
//        //加入下载队列后会给该任务返回一个long型的id，
//        //通过该id可以取消任务，重启任务等等，看上面源码中框起来的方法
//        mTaskId = downloadManager.enqueue(request);
//
//        //注册广播接收者，监听下载状态
//        MainActivity.this.registerReceiver(receiver,
//                new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
//    }
//
//    //接下来是广播接收器
//
//    //广播接受者，接收下载状态
//    private BroadcastReceiver receiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            checkDownloadStatus();//检查下载状态
//        }
//    };
//
//    //检查下载状态
//
//    //检查下载状态
//    private void checkDownloadStatus() {
//        DownloadManager.Query query = new DownloadManager.Query();
//        query.setFilterById(mTaskId);//筛选下载任务，传入任务ID，可变参数
//        Cursor c = downloadManager.query(query);
//        if (c.moveToFirst()) {
//            int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
//            switch (status) {
//                case DownloadManager.STATUS_PAUSED:
//                    Log.i("a",">>>下载暂停");
//                case DownloadManager.STATUS_PENDING:
//                    Log.i("a",">>>下载延迟");
//                case DownloadManager.STATUS_RUNNING:
//                    Log.i("a",">>>正在下载");
//                    break;
//                case DownloadManager.STATUS_SUCCESSFUL:
//                    Log.i("a",">>>下载完成");
//                    //下载完成安装APK
////                    downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator + versionName;
////                    installAPK(new File(downloadPath));
//                    break;
//                case DownloadManager.STATUS_FAILED:
//                    Log.i("a",">>>下载失败");
//                    break;
//            }
//        }
//    }
//
//    //安装APK
//
//    //下载到本地后执行安装
//    protected void installAPK(File file) {
//        if (!file.exists()) return;
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        Uri uri = Uri.parse("file://" + file.toString());
//        intent.setDataAndType(uri, "application/vnd.android.package-archive");
//        //在服务中开启activity必须设置flag,后面解释
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        MainActivity.this.startActivity(intent);
//    }
}
